package com.lockmem.constants;

import com.lockmem.ctrl.CtrlService;
import com.lockmem.listener.ListenerService;
import com.lockmem.study.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

public abstract class MyActivity extends Activity {
	
	private IntentFilter filter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
		startLoadingDlg();
		startService(new Intent(this,ListenerService.class));
		bindService(new Intent(this, CtrlService.class), conn,
				Context.BIND_AUTO_CREATE);
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unbindService(conn);
//		dismissDialog(R.layout.loading_dlg);
		Log.v("1","onDestroy");
		super.onDestroy();
	}

	/**
	 * 用于显示Loading，等待更新界面
	 */
	protected Handler loading_handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {// 0表示显示Dialog开始更新，1表示更新完毕
			case 0:
				loading();
				break;
			case 1:
				loadingdlg.cancel();
				break;
			}
		}
	};
	private AlertDialog loadingdlg;
	protected LayoutInflater inflater;

	private void loading() {
		LayoutInflater inflater = getInflater();
		final View v = inflater.inflate(R.layout.loading_dlg, null);
		loadingdlg = new AlertDialog.Builder(MyActivity.this).setView(v).show();
	}
	protected void startLoadingDlg(){
	loading_handler.obtainMessage(0).sendToTarget(); // 开始更新UI
	}
	protected void stopLoadingDlg(){
		loading_handler.obtainMessage(1).sendToTarget();
	}
	

	
	/**
	 * 将主题改为男生
	 * @param view 要改的控件
	 * @param bkg_num 改的颜色，0~4代表bkg_boy~bkg_boy_4
	 */
	protected void turnBoyTheme(View view,int bkg_num) {
		switch (bkg_num) {
		case 0:
			view.setBackgroundResource(R.color.bkg_boy);	
			break;
		case 1:
			view.setBackgroundResource(R.color.bkg_boy_1);	
			break;
		case 2:
			view.setBackgroundResource(R.color.bkg_boy_2);	
			break;
		case 3:
			view.setBackgroundResource(R.color.bkg_boy_3);	
			break;
		case 4:
			view.setBackgroundResource(R.color.bkg_boy_4);	
			break;
		}
	}

	/**
	 * 将主题改为男生
	 * @param ViewId 要改的控件Id
	 * @param bkg_num 改的颜色，0~4代表bkg_boy~bkg_boy_4
	 */
	protected void turnBoyTheme(int ViewId,int bkg_num) {
		View view = (View)findViewById(ViewId);
		turnBoyTheme(view, bkg_num);
	}
	protected void  turnBoyTheme(int LayoutId,int ViewId,int bkg_num) {
		View view = getInflater().inflate(LayoutId,null).findViewById(ViewId);
		turnBoyTheme(view, bkg_num);
	}
	
	/**
	 * 将主题改为女生
	 * @param view
	 * @param bkg_num
	 */
	protected void turnGirlTheme(View view,int bkg_num) {
		switch (bkg_num) {
		case 0:
			view.setBackgroundResource(R.color.bkg_girl);	
			break;
		case 1:
			view.setBackgroundResource(R.color.bkg_girl_1);	
			break;
		case 2:
			view.setBackgroundResource(R.color.bkg_girl_2);	
			break;
		case 3:
			view.setBackgroundResource(R.color.bkg_girl_3);	
			break;
		case 4:
			view.setBackgroundResource(R.color.bkg_girl_4);	
			break;
		}
	}
	protected void  turnGirlTheme(int ViewId,int bkg_num) {
		turnGirlTheme(findViewById(ViewId), bkg_num);
	}	
	protected LayoutInflater getInflater() {
		if (inflater ==null) {
			inflater = LayoutInflater.from(this);
		}
		return inflater;
	}
	public abstract void init();	
	protected CtrlService ser = null;
	public ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			ser = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			ser = ((CtrlService.MyBinder) service).getService();
			init();
			setTheme();
			stopLoadingDlg();
		}
	};
	public abstract void setTheme();
}
